
package com.bootx.entity;

import com.fasterxml.jackson.annotation.JsonView;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotEmpty;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Length;

import java.util.*;

/**
 * @author black
 */
@Entity
public class Product extends BaseEntity<Long> {

	@NotEmpty
	@Column(nullable = false,unique = true)
	private String otherUrl;

    @JsonView({ViewView.class})
	private String name;

    @JsonView({ViewView.class})
	private String memo;

	private String otherId;

    @JsonView({ViewView.class})
	private String image;

	/**
	 * 商品分类
	 */
	@ManyToMany(fetch = FetchType.LAZY)
	private Set<ProductCategory> productCategories = new HashSet<>();

    private Date publishDate;

    @Column(columnDefinition = "longtext")
    @JsonView({ViewView.class})
    private String content;

    @JsonView({ViewView.class})
    private String icon;

    @JsonView({ViewView.class})
    private String logo;

    /**
     * 页面描述
     */
    @Length(max = 200)
    @JsonView({ViewView.class})
    private String description;

    /**
     * 页面标题
     */
    @Length(max = 200)
    @JsonView({ViewView.class})
    private String seoTitle;

    /**
     * 页面关键词
     */
    @JsonView({ViewView.class})
    private String seoKeywords;

    /**
     * 页面描述
     */
    @JsonView({ViewView.class})
    private String seoDescription;

    public Set<ProductCategory> getProductCategories() {
        return productCategories;
    }

    public void setProductCategories(Set<ProductCategory> productCategories) {
        this.productCategories = productCategories;
    }

	public String getOtherUrl() {
		return otherUrl;
	}

	public void setOtherUrl(String otherUrl) {
		this.otherUrl = otherUrl;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

	public String getOtherId() {
		return otherId;
	}

	public void setOtherId(String otherId) {
		this.otherId = otherId;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

    public Date getPublishDate() {
        return publishDate;
    }

    public void setPublishDate(Date publishDate) {
        this.publishDate = publishDate;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public String getLogo() {
        return logo;
    }

    public void setLogo(String logo) {
        this.logo = logo;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * 获取页面标题
     *
     * @return 页面标题
     */
    public String getSeoTitle() {
        return seoTitle;
    }

    /**
     * 设置页面标题
     *
     * @param seoTitle
     *            页面标题
     */
    public void setSeoTitle(String seoTitle) {
        this.seoTitle = seoTitle;
    }

    /**
     * 获取页面关键词
     *
     * @return 页面关键词
     */
    public String getSeoKeywords() {
        return seoKeywords;
    }

    /**
     * 设置页面关键词
     *
     * @param seoKeywords
     *            页面关键词
     */
    public void setSeoKeywords(String seoKeywords) {
        if (seoKeywords != null) {
            seoKeywords = seoKeywords.replaceAll("[,\\s]*,[,\\s]*", ",").replaceAll("^,|,$", StringUtils.EMPTY);
        }
        this.seoKeywords = seoKeywords;
    }

    /**
     * 获取页面描述
     *
     * @return 页面描述
     */
    public String getSeoDescription() {
        return seoDescription;
    }

    /**
     * 设置页面描述
     *
     * @param seoDescription
     *            页面描述
     */
    public void setSeoDescription(String seoDescription) {
        this.seoDescription = seoDescription;
    }


    @Transient
    @JsonView({ViewView.class})
    public List<Map<String,Object>>  getProductCategoriesNames() {
        List<Map<String,Object>> productCategoriesNames = new ArrayList<>();
        for (ProductCategory productCategory : productCategories) {
            Map<String,Object> map = new HashMap<>();
            map.put("name", productCategory.getName());
            map.put("url","/category/"+productCategory.getId());
            productCategoriesNames.add(map);
        }
        return productCategoriesNames;
    }

}